﻿//using iText.Kernel.Pdf;
//using iText.Kernel.Pdf.Canvas.Parser;
using Aspose.Pdf;
using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Xceed.Words.NET;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace ImageToPdf
{
    public partial class PDF2WordForm : Form
    {
        public PDF2WordForm()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "PDF Files|*.pdf";
            openFileDialog.Multiselect = false;
            openFileDialog.CheckFileExists = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                lbFile.Text = openFileDialog.FileName;
            }
        }

        private void btnToWord_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(lbFile.Text) || !lbFile.Text.ToLower().EndsWith(".pdf"))
            {
                MessageBox.Show("请先选择一个PDF文件");
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Word Files|*.docx";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ConvertPdfToWord3(saveFileDialog.FileName);
                MessageBox.Show("PDF生成成功");

            }
        }

        //private void ConvertPdfToWord(string wordPath)
        //{
        //    using (var doc = DocX.Create(wordPath))
        //    {
        //        using (PdfReader pdfReader = new PdfReader(lbFile.Text))
        //        using (PdfDocument pdfDocument = new PdfDocument(pdfReader))
        //        {
        //            for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++)
        //            {
        //                string text = PdfTextExtractor.GetTextFromPage(pdfDocument.GetPage(i));
        //                doc.InsertParagraph(text);
        //            }
        //        }
        //        doc.Save();
        //    } 
        //}

        private void ConvertPdfToWord2(string wordPath)
        {
            // 加载 PDF 文件
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(lbFile.Text);

            // 保存为 Word 文档
            pdf.SaveToFile(wordPath, FileFormat.DOCX);
            pdf.Close();
        }
        private void ConvertPdfToWord3(string wordPath)
        {
            // 加载 PDF 文件
            Document pdfDocument = new Document(lbFile.Text);

            // 保存为 Word 文档
            pdfDocument.Save(wordPath, SaveFormat.DocX);
        }
    }
}
